import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

//公共底部选择弹窗

//代码调用示例 在点击事件中调用
//showModalBottomSheet(
//      context: context,
//        shape: RoundedRectangleBorder(
//              borderRadius: BorderRadiusDirectional.circular(0)
//         ),
//       builder: (BuildContext context){
//            return AlertBottomPage(
//                    items: ['相机','相册'],
//           chooseCallback: (index){
//               print('当前点击的索引值$index');
//          },
//       );
//   });
class AlertBottomPage extends StatefulWidget {
  final List<String> items;
  final ValueChanged<int> chooseCallback;
  int _selectIndex = 0;
  final List<ListTile> ListItems;
  AlertBottomPage({this.items, this.chooseCallback, this.ListItems});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AlertBottomPageState(
        items: items, chooseCallback: chooseCallback, ListItems: ListItems);
  }
}

class _AlertBottomPageState extends State<AlertBottomPage> {
  final List<String> items;
  final ValueChanged<int> chooseCallback;
  int _selectIndex = 0;
  final List<ListTile> ListItems;
  _AlertBottomPageState({this.items, this.chooseCallback, this.ListItems});

  Widget TopView() {
    return new Stack(
      children: <Widget>[
        Container(
          width: ScreenUtil.screenWidth,
          height: ScreenUtil().setWidth(52.5),
          color: Color(0xffF2F2F2),
        ),
        Positioned(
          child: Container(
            child: InkWell(
              child: Text(
                '关闭',
                style: TextStyle(
                    color: Colors.black, fontSize: ScreenUtil().setSp(16)),
              ),
              onTap: () {
                Navigator.pop(context);
              },
            ),
          ),
          left: ScreenUtil().setWidth(15),
          top: ScreenUtil().setHeight(18.6),
        ),
        Positioned(
          child: Container(
            child: InkWell(
              child: Text(
                '确定',
                style: TextStyle(
                    color: Colors.black, fontSize: ScreenUtil().setSp(16)),
              ),
              onTap: () {
                chooseCallback(_selectIndex);
                Navigator.pop(context);
              },
            ),
          ),
          right: ScreenUtil().setWidth(15),
          top: ScreenUtil().setHeight(18.6),
        ),
      ],
    );
  }

  List<Widget> itemsWidget(context) {
    List<Widget> list = new List();
    list.add(TopView());

    list.add(Container(
        width: ScreenUtil().setWidth(1024),
        height: ScreenUtil().setWidth(261),
       // color: Colors.green,
        child: ListView.separated(
          itemBuilder: (context, index) {
            return ListTile(
              title: FlatButton(
                child: Text(
                  items[index],
                  style: TextStyle(
                      color:
                          _selectIndex == index ? Colors.blue : Colors.black),
                ),
                onPressed: () {
                 // chooseCallback(index);
                  setState(() {
                    _selectIndex = index;
                  });
                },
              ),
            );
          },
          separatorBuilder: (context,index){
            return Divider(height: 1,);
          },
          itemCount: items.length,
        )));

//    for (int i =0;i<items.length;i++){
//      list.add( ListTile(
//        title: FlatButton(
//          child: Text(items[i],style: TextStyle(color: _selectIndex==i?Colors.blue:Colors.black),),
//          onPressed: (){
//            chooseCallback(i);
//            setState(() {
//              _selectIndex =i;
//            });
//
//          },
//        ),
//
//      ));
//      list.add(Divider(height: 1,));
//    }

//    list.add(SizedBox(child: Container(color: Color(0xffEFEFF4),),height: 8,),);
//    list.add(ListTile(
//      title: FlatButton(child: Text('取消'),
//        onPressed: (){
//          Navigator.pop(context);
//        },
//      ),
//    ));
    return list;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Column(
      // mainAxisSize: MainAxisSize.min,
      children: itemsWidget(context),
    );
  }
}
